package com.goods.web.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.goods.pojo.IptvUserBean;
import com.goods.service.IIptvUserService;
import com.goods.util.CommonTools;
import com.somtacode.core.base.result.ResponseDataResult;
import com.somtacode.core.utils.CommonUtil;

@RestController
@RequestMapping("/iptvUser")
public class IptvUserController {

	@Autowired
	private IIptvUserService iptvUserService;
	/**
	 * 新增机顶盒账号
	 * @param IptvUser
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/addIptvUser")
    public ResponseDataResult addIptvUser(IptvUserBean iptvUser) throws Exception{
        if(CommonUtil.isNullOrEmpty(iptvUser)){
    		return ResponseDataResult.setErrorResponseResult("IptvUser不能为空");
    	}
        iptvUser.setRegistertime(new Date());
        return iptvUserService.addIptvUser(iptvUser);
    }


	/**
	 * 删除机顶盒账号
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/deleteIptvUser")
    public ResponseDataResult deleteIptvUser(Integer id) throws Exception{
    	if(CommonUtil.isNullOrEmpty(id)){
    		return ResponseDataResult.setErrorResponseResult("id不能为空");
    	}
    	try {
    		iptvUserService.deleteIptvUser(id);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
    	return ResponseDataResult.setResponseResult();
    }


	/**
	 * 修改机顶盒账号
	 * @param IptvUser
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/updateIptvUser")
	public ResponseDataResult updateIptvUser(IptvUserBean iptvUser) throws Exception{
		if(CommonUtil.isNullOrEmpty(iptvUser)){
			return ResponseDataResult.setErrorResponseResult("IptvUser不能为空");
		}
		return iptvUserService.updateIptvUser(iptvUser);
	}
	
	/**
	 * 根据id获取机顶盒账号
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/getIptvUser")
    public ResponseDataResult getIptvUser(Integer id) throws Exception{
		List<IptvUserBean> iptvUser = null;
		try {
			iptvUser = iptvUserService.getIptvUser(id);
    		if(CommonUtil.isNullOrEmpty(iptvUser)){
        		return ResponseDataResult.setErrorResponseResult("IptvUser不存在，请重新输入");
        	}
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return ResponseDataResult.setResponseResult(iptvUser);
    }
	
	/**
	 * 获取机顶盒账号列表
	 * @param p
	 * @param size
	 * @param name
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/listIptvUser")
	public List<IptvUserBean> listIptvUser(Integer p,Integer size,String name) throws Exception {
		p = p == null ? 1 : p;
		size = size == null ? 20 :size;
		IptvUserBean iptvUser = new IptvUserBean();
		if(!CommonTools.isNullOrEmpty(name)){
			iptvUser.setName(name);
		}
		PageInfo<IptvUserBean> page = iptvUserService.listIptvUser(p, size,iptvUser);
		return page.getList();
	}
	
}
